﻿using NetEasyMusic.Lib.Models;
using NetEasyMusic.Lib.Recommend;

// ReSharper disable StringLiteralTypo

namespace NetEasyMusic.Lib.Data;

public class Radio : DataBasic
{
    public Radio(string cookie) : base(cookie)
    {
    }

    public Radio()
    {
    }

    public Radio(string url, bool isUse = false) : base(url, isUse)
    {
    }

    /// <summary>
    ///     推荐电台
    /// </summary>
    /// <returns></returns>
    public async Task<DjProgramRecommend[]> DjProgram()
    {
        var context = await GetFormString("/personalized/djprogram");
        return context?["result"]?.Select(x => new DjProgramRecommend(x)).ToArray() ?? [];
    }

    public async Task<DjProgramRecommend[]> GetUserRadio(string id) // 
    {
        var obj = await GetFormString($"/user/dj?uid={id}&cookie={Cookie}");
        return obj?["programs"]!.Select(x => new DjProgramRecommend(x)).ToArray() ?? [];
    }

    public async Task<DjProgramRecommend[]> TopList()
    {
        var obj = await GetFormString("/dj/program/toplist");
        return obj?["toplist"]!.Select(x =>
        {
            var token = x["program"];
            return new DjProgramRecommend
            {
                Id = token!["id"]!.ToObject<string>()!,
                Name = token["name"]!.ToObject<string>()!,
                Copywriter = token["description"]!.ToObject<string>()!,
                PicUrl = token["coverUrl"]!.ToObject<string>()!,
                DjNickname = token["dj"]!["nickname"]!.ToObject<string>()!,
                DjSignature = token["dj"]!["signature"]!.ToObject<string>()!,
                DjAvatarUrl = token["dj"]!["avatarUrl"]!.ToObject<string>()!,
                Brand = token["dj"]!["brand"]!.ToObject<string>()!,
                RadioId = token["radio"]!["id"]!.ToObject<string>()!,
                RadioName = token["radio"]!["name"]!.ToObject<string>()!,
                RadioPicUrl = token["radio"]!["picUrl"]!.ToObject<string>()!,
                RadioDesc = token["radio"]!["desc"]!.ToObject<string>()!,
                Channels = token["channels"]!.ToObject<string[]>()!,
                Description = token["description"]!.ToObject<string>()!
            };
        }).ToArray() ?? [];
    }

    public async Task<DjProgramRecommend[]> PayList()
    {
        var obj = await GetFormString("/dj/toplist/pay");
        return obj?["data"]!["list"]!.Select(x => new DjProgramRecommend
        {
            Id = x["id"]!.ToObject<string>()!,
            Name = x["name"]!.ToObject<string>()!,
            PicUrl = x["picUrl"]!.ToObject<string>()!
        }).ToArray() ?? [];
    }

    public async Task<DjProgramRecommend[]> Top24()
    {
        var obj = await GetFormString("/dj/program/toplist/hours");
        return obj?["data"]!["list"]!.Select(x =>
        {
            var token = x["program"];
            return new DjProgramRecommend
            {
                Id = token!["id"]!.ToObject<string>()!,
                Name = token["name"]!.ToObject<string>()!,
                Copywriter = token["description"]!.ToObject<string>()!,
                PicUrl = token["coverUrl"]!.ToObject<string>()!,
                DjNickname = token["dj"]!["nickname"]!.ToObject<string>()!,
                DjSignature = token["dj"]!["signature"]!.ToObject<string>()!,
                DjAvatarUrl = token["dj"]!["avatarUrl"]!.ToObject<string>()!,
                Brand = token["dj"]!["brand"]!.ToObject<string>()!,
                RadioId = token["radio"]!["id"]!.ToObject<string>()!,
                RadioName = token["radio"]!["name"]!.ToObject<string>()!,
                RadioPicUrl = token["radio"]!["picUrl"]!.ToObject<string>()!,
                RadioDesc = token["radio"]!["desc"]!.ToObject<string>()!,
                Channels = token["channels"]!.ToObject<string[]>()!,
                Description = token["description"]!.ToObject<string>()!
            };
        }).ToArray() ?? [];
    }

    public async Task<UserModel[]> Pay24()
    {
        var obj = await GetFormString("/dj/toplist/hours");

        return obj?["data"]!["list"]!.Select(x => new UserModel
        {
            UserId = x["id"]!.ToObject<string>()!,
            Nickname = x["nickName"]!.ToObject<string>()!,
            AvatarUrl = x["avatarUrl"]!.ToObject<string>()!
        }).ToArray() ?? [];
    }
}